/**
 * 
 */
package com.aspire.wap.core.io;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;



/**
 * <p>打包工具类</p>
 * @author sunquanzhi
 * @version 1.0
 */
public class ZipUtil {
	
	public static boolean isZipFile( File file )
	{
		if( file == null || !file.exists() )
		{
			return false;
		}
		else if( StringUtils.endsWith(file.toString(), ".zip") )
		{
			return true;
		}
		else
		{
			return false;
		}		
	}
	
	/**
	 * 解压缩文件
	 * @param zipFile   
	 * @param outFilePath
	 * @param mode    if 1解压缩到当前outFilePath，else解压缩到outFilePath+zip文件名目录 
	 */
	public static void unZipFile(String zipFile,String outFilePath,int mode)
	{
		try{
			File file = new File(zipFile);
			String fileName = file.getName();
			if(mode == 1)
			{
				outFilePath += File.separator;
			}else{
				outFilePath += File.separator 
					+fileName.substring(0 , fileName.length() -4)
					+ File.separator;
				
			}
			File tmpFileDir = new File(outFilePath);
			tmpFileDir.mkdirs();
			
			ZipFile zf = new ZipFile(zipFile);
			FileOutputStream fos;
			
			byte[] buf = new byte[1024];
			for(Enumeration em = zf.getEntries() ; em.hasMoreElements();)
			{
				ZipEntry ze = (ZipEntry) em.nextElement();
				if(ze.isDirectory())
				{
					continue;
				}
				DataInputStream dis = new DataInputStream(zf.getInputStream(ze) );
				String currentFileName = ze.getName();
				int dex = currentFileName.lastIndexOf(File.separator);
				String currentoutFilePath = outFilePath;
				if(dex > 0)
				{
					currentoutFilePath += currentFileName.substring(0,dex)+File.separator;
					File currentFileDir = new File(currentoutFilePath);
					currentFileDir.mkdirs();
				}
				String saveFilePath = outFilePath + ze.getName ( );
				
				saveFilePath = saveFilePath.replaceAll("\\\\","/");
				saveFilePath = saveFilePath.substring(0,saveFilePath.lastIndexOf("/"));
				new File(saveFilePath).mkdirs();
				//在新的位置创建zip包中含有的子目录
				
				fos = new FileOutputStream(outFilePath + ze.getName ( ));
				int readLen = 0;
				while( (readLen = dis.read(buf,0,1024)) > 0 )
				{
					fos.write(buf , 0 ,readLen);
				}
				dis.close();
				fos.close();
				
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 压缩指定目录下的文件到一个zip文件里
	 * @param strFilePath 指定目录地址
	 * @param outFileName 目的zip文件地址
	 * @param mode     if 1压缩到当前outFileName，else压缩到outFileName+目录名称      
	 */
	public static void zipFile(String strFilePath,String outFileName,int mode)
	{
		try{
			File file = new File(strFilePath);
			String fileName = file.getName();
			if(mode != 1)
			{
				if(fileName.indexOf(".")>0)
				{
					fileName = fileName.substring(0,fileName.lastIndexOf("."));
				}
				outFileName += File.separator 
					+fileName ;	
			}
			int dex = outFileName.lastIndexOf(File.separator);
			if(dex > 0)
			{
				File outFilePath = new File(outFileName.substring(0,dex));
				outFilePath.mkdirs();
			}
			outFileName += ".zip";
			
			FileOutputStream fos = new FileOutputStream(outFileName);
			ZipOutputStream zos = new ZipOutputStream(fos);
			try{
				putEntry(file,zos,"");
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			zos.close();
			fos.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
			
	}
	private static void putEntry(File file,ZipOutputStream zos,String path)throws Exception
	{
		if(file.isDirectory())
		{
			File files[] = file.listFiles();
			
			for(int i = 0; i<files.length ; i++)
			{
				putEntry(files[i],zos,path+file.getName()+File.separator);
			}
		}else if(file.isFile()){
			
			ZipEntry ze = new ZipEntry(path+file.getName());
			zos.putNextEntry(ze);
			InputStream in = new FileInputStream(file);
			byte buf[] = new byte[1024];
			int len = 0;
			while((len=in.read(buf,0,1024))>0)
			{
				zos.write(buf,0,len);
			}
			zos.closeEntry();
			 
		}
	}
}
